//
//  LoginViewController.m
//  Kiwi
//
//  Created by Herron on 16/8/16.
//  Copyright © 2016年 Herron. All rights reserved.
//

#import "LoginViewController.h"
#import "AppDelegate.h"
#import "LXDScanCodeController.h"
#import "RegisterViewController.h"

@interface LoginViewController ()<LXDScanCodeControllerDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
@property (nonatomic, copy) NSString *code;

@end

@implementation LoginViewController

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - <LXDScanCodeControllerDelegate>
- (void)scanCodeController: (LXDScanCodeController *)scanCodeController codeInfo: (NSString *)codeInfo {
    NSLog(@"codeInfo = %@", codeInfo);
    [NetWork postNetWorkWithUrl:kURLScanInvitationCode parame:@{@"invitationCode":codeInfo, @"type":@"2"} successBlock:^(NSDictionary *dic) {
        NSLog(@"dic = %@", [dic description]);
        if ([dic[@"status"] isEqualToNumber:@(0)]) {
            NSNumber *state = dic[@"data"][@"state"];
            if ([state isEqualToNumber:@(101)]) {
                [scanCodeController.scanView stop];
                [scanCodeController.scanView removeFromSuperview];
                RegisterViewController *vc = [[RegisterViewController alloc] init];
                vc.invitationCode = codeInfo;
                [scanCodeController.navigationController pushViewController:vc animated:YES];
            } else {
                UIAlertController *controller = [UIAlertController alertControllerWithTitle:dic[@"data"][@"message"] message:nil preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"继续" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [scanCodeController.scanView start];
                }];
                UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [scanCodeController dismissViewControllerAnimated:YES completion:nil];
                }];
                [controller addAction:action2];
                [controller addAction:action1];
                [scanCodeController presentViewController:controller animated:YES completion:nil];
            }
        } else {
            [HUDManager showWarningWithText:@"请求失败"];
            [scanCodeController.scanView start];
        }
    } errorBlock:^(id error) {
        [HUDManager showWarningWithText:@"网络异常"];
        [scanCodeController.scanView start];
    }];
}

#pragma mark - event mothed
/**
 *  登录
 */
- (IBAction)loginClick:(id)sender {
    if (self.phoneTextField.text.length == 0) {
        [HUDManager showWarningWithText:@"请输入手机号"];
        return;
    }
    if (self.pwdTextField.text.length == 0) {
        [HUDManager showWarningWithText:@"请输入验证码"];
        return;
    }
    if ([self.phoneTextField.text validateMobile]) {
        [NetWork postNetWorkWithUrl:kURLUserLogin parame:@{@"phoneNumber":self.phoneTextField.text,@"verifyCode":self.code} successBlock:^(NSDictionary *dic) {
            NSLog(@"登录 = %@", dic);
            if([dic[@"status"] integerValue] == 0) {
                // TODO: 登录成功
                [[UserAccountManager shareUserManager] loginWithUserDic:dic[@"data"]];
                [UIApplication sharedApplication].keyWindow.rootViewController = [(AppDelegate*)[UIApplication sharedApplication].delegate homeController];
                [(AppDelegate*)[UIApplication sharedApplication].delegate noticeServicerPushClientId];
            } else {
                [HUDManager showWarningWithText:dic[@"message"]];
            }
        } errorBlock:^(id error) {
            [HUDManager showWarningWithText:@"网络异常"];
        }];
    }
}

/**
 *  获取验证码
 */
- (IBAction)getVerificationCode:(id)sender {
    if (self.phoneTextField.text.length == 0) {
        [HUDManager showWarningWithText:@"请输入手机号"];
        return;
    }
    if ([self.phoneTextField.text validateMobile]) {
        [NetWork postNetWorkWithUrl:@"get-verifycode" parame:@{@"phoneNumber":self.phoneTextField.text} successBlock:^(NSDictionary *dic) {
            NSLog(@"登录验证码 = %@", dic);
            self.code = dic[@"data"][@"verifyCode"];
        } errorBlock:^(id error) {
            [HUDManager showWarningWithText:@"网络异常"];
        }];
        [sender timeCountDown];
    }
}

/**
 *  去注册
 */
- (IBAction)registerClick:(id)sender {
    [NetWork postNetWorkWithUrl:kURLNeedPassengerQrCode parame:nil successBlock:^(NSDictionary *dic) {
        NSLog(@"是否需要推荐码 = %@", dic);
        NSNumber *isSuccessful = dic[@"data"][@"need"];
        if ([isSuccessful isEqualToNumber:@(0)]) {
            NSString *qrcode = dic[@"data"][@"qrcode"];
            RegisterViewController *registerVC = [[RegisterViewController alloc] init];
            registerVC.invitationCode = qrcode;
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:registerVC];
            [self presentViewController:nav animated:YES completion:nil];
        } else {
            LXDScanCodeController *vc = [[LXDScanCodeController alloc] init];
            vc.scanDelegate = self;
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
            [self presentViewController:nav animated:YES completion:nil];
        }
    } errorBlock:^(id error) {
        [HUDManager showWarningWithText:kMessage_NetError];
    }];
}



@end
